package com.itr.kbm.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.itr.kbm.controller.attribute.MainAttribute;
import com.itr.kbm.dao.FileStatusDao;
import com.itr.kbm.dao.ListKopegDao;
import com.itr.kbm.dao.ListKoptelProductDao;
import com.itr.kbm.model.Employee;
import com.itr.kbm.model.FileStatus;
import com.itr.kbm.model.Kopeg;
import com.itr.kbm.model.KoptelProduct;
import com.itr.kbm.model.ListKopeg;
import com.itr.kbm.model.ListKoptelProduct;
import com.itr.kbm.model.SessionDataObject;
import com.itr.kbm.service.ExcelService;

@Controller
public class MainController {

	private static final String INDEX = "index";
	private static final String BILLING = "billing";
	private static final String CHANGE_PASSWORD = "change_password";
	private static final String LOGIN = "login";

	// KOPEG
	private static final String UPLOAD_KOPEG = "upload_kopeg";
	private static final String DOWNLOAD_FINAL_REPORT = "download_final_report";

	private static final String MERGE = "merge";
	private static final String UPLOAD_COMPARE = "upload_compare";
	private static final String COMPARE = "compare";
	private static final String UPLOAD_FINAL_REPORT = "upload_final_report";

	private static final String SPLIT = "split";

	private static final String UPLOAD_EMPLOYEE = "upload_employee";
	private static final String UPLOAD_LIST_KOPEG = "upload_list_kopeg";

	// KOPTEL PRODUCT
	private static final String UPLOAD_KOPTEL_PRODUCT = "upload_koptel_product";
	private static final String DOWNLOAD_FINAL_REPORT_PRODUCT = "download_final_report_product";

	private static final String MERGE_PRODUCT = "merge_product";
	private static final String UPLOAD_COMPARE_PRODUCT = "upload_compare_product";
	private static final String COMPARE_PRODUCT = "compare_product";
	private static final String UPLOAD_FINAL_REPORT_PRODUCT = "upload_final_report_product";

	private static final String SPLIT_PRODUCT = "split_product";

	private static final String UPLOAD_LIST_KOPTEL_PRODUCT = "upload_list_koptel_product";

	private HttpSession session = null;
	private SessionDataObject object = null;

	private MainAttribute form;

	@Autowired
	private ExcelService excelSvc;

	@Autowired
	public FileStatusDao fileStatusDao;

	@Autowired
	public ListKopegDao listKopegDao;

	@Autowired
	public ListKoptelProductDao listKoptelProductDao;

	@RequestMapping("/")
	public ModelAndView home(ModelAndView mav) {

		mav.setViewName(BILLING);
		return mav;
	}

	@RequestMapping("/billing")
	public ModelAndView billing(ModelAndView mav) {

		mav.setViewName(BILLING);
		return mav;
	}

	@RequestMapping("/change_password")
	public ModelAndView changePassword(ModelAndView mav, HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(CHANGE_PASSWORD);
		return mav;
	}

	@RequestMapping("/login")
	public ModelAndView login(ModelAndView mav) {

		mav.setViewName(LOGIN);
		return mav;
	}

	// KOPEG
	@RequestMapping("/upload_kopeg")
	public ModelAndView upload_kopeg(ModelAndView mav, HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		
		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_KOPEG);
		return mav;
	}

	@RequestMapping("/download_final_report")
	public ModelAndView download_final_report(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(DOWNLOAD_FINAL_REPORT);
		return mav;
	}

	@RequestMapping("/merge")
	public ModelAndView merge(ModelAndView mav, HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(MERGE);
		return mav;
	}

	@RequestMapping("/upload_compare")
	public ModelAndView upload_compare(ModelAndView mav, HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_COMPARE);
		return mav;
	}

	@RequestMapping("/compare")
	public ModelAndView compare(ModelAndView mav, HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(COMPARE);
		return mav;
	}

	@RequestMapping("/upload_final_report")
	public ModelAndView upload_final_report(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_FINAL_REPORT);
		return mav;
	}

	@RequestMapping("/split")
	public ModelAndView split(ModelAndView mav, HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(SPLIT);
		return mav;
	}

	@RequestMapping("/upload_employee")
	public ModelAndView upload_employee(ModelAndView mav, HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_EMPLOYEE);
		return mav;
	}

	@RequestMapping("/upload_list_kopeg")
	public ModelAndView upload_list_kopeg(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_LIST_KOPEG);
		return mav;
	}

	// KOPTEL PRODUCT
	@RequestMapping("/upload_koptel_product")
	public ModelAndView upload_koptel_product(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_KOPTEL_PRODUCT);
		return mav;
	}

	@RequestMapping("/download_final_report_product")
	public ModelAndView download_final_report_product(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(DOWNLOAD_FINAL_REPORT_PRODUCT);
		return mav;
	}

	@RequestMapping("/merge_product")
	public ModelAndView merge_product(ModelAndView mav, HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(MERGE_PRODUCT);
		return mav;
	}

	@RequestMapping("/upload_compare_product")
	public ModelAndView upload_compare_product(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_COMPARE_PRODUCT);
		return mav;
	}

	@RequestMapping("/compare_product")
	public ModelAndView compare_product(ModelAndView mav, HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(COMPARE_PRODUCT);
		return mav;
	}

	@RequestMapping("/upload_final_report_product")
	public ModelAndView upload_final_report_product(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_FINAL_REPORT_PRODUCT);
		return mav;
	}

	@RequestMapping("/split_product")
	public ModelAndView split_product(ModelAndView mav, HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(SPLIT_PRODUCT);
		return mav;
	}

	@RequestMapping("/upload_list_koptel_product")
	public ModelAndView upload_list_koptel_product(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		form = new MainAttribute();
		mav.addObject("form", form);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_LIST_KOPTEL_PRODUCT);
		return mav;
	}

	@RequestMapping("/request-login")
	public ModelAndView requestLogin(ModelAndView mav, HttpServletRequest req) {

		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String alert = "";
		String icon = "";

		String kodeKopeg = ""; 
		String user = "0"; // not used
		String typeUser = ""; // kopeg, adminKopeg, koptelProduct, adminKoptelProduct, admin
		String kodeKoptelProduct = ""; 

		session = req.getSession();
		object = new SessionDataObject();

		if (excelSvc.checkInListAdmin(username, password)) {
			if (username.equals("ADM001")) {
				kodeKopeg = username; // admin nebeng di kopeg
				typeUser = "admin";

				object.setUser(user);
				object.setTypeUser(typeUser);
				object.setKodeKopeg(kodeKopeg);
				object.setKodeKoptelProduct(kodeKoptelProduct);
				session.setAttribute("logindata", object);

				form = new MainAttribute();
				mav.addObject("form", form);

				// mav.addObject("user", user);
				// mav.addObject("kodeKopeg", kodeKopeg);
				// mav.addObject("type", typeUser);

				mav.setViewName(UPLOAD_EMPLOYEE);
				return mav;
			} else {
				if (username.equals("ADM002")) {
					kodeKopeg = username; // adminKopeg nebeng di kopeg
					typeUser = "adminKopeg";

					object.setUser(user);
					object.setTypeUser(typeUser);
					object.setKodeKopeg(kodeKopeg);
					object.setKodeKoptelProduct(kodeKoptelProduct);
					session.setAttribute("logindata", object);

					form = new MainAttribute();
					mav.addObject("form", form);

					// mav.addObject("user", user);
					// mav.addObject("kodeKopeg", kodeKopeg);
					// mav.addObject("type", typeUser);

					mav.setViewName(MERGE);
					return mav;
				} else {
					if (username.equals("ADM003")) {
						kodeKoptelProduct = username; // adminKoptelProduct
														// nebeng di
														// koptelProduct
						typeUser = "adminKoptelProduct";

						object.setUser(user);
						object.setTypeUser(typeUser);
						object.setKodeKopeg(kodeKopeg);
						object.setKodeKoptelProduct(kodeKoptelProduct);
						session.setAttribute("logindata", object);

						form = new MainAttribute();
						mav.addObject("form", form);

						// mav.addObject("user", user);
						// mav.addObject("kodeKoptelProduct",
						// kodeKoptelProduct);
						// mav.addObject("type", typeUser);

						mav.setViewName(MERGE_PRODUCT);
						return mav;
					}
				}
			}
		} else {
			if (excelSvc.checkInListKopeg(username, password)) {
				kodeKopeg = username;
				typeUser = "kopeg";

				object.setUser(user);
				object.setTypeUser(typeUser);
				object.setKodeKopeg(kodeKopeg);
				object.setKodeKoptelProduct(kodeKoptelProduct);
				session.setAttribute("logindata", object);

				form = new MainAttribute();
				mav.addObject("form", form);

				// mav.addObject("user", user);
				// mav.addObject("kodeKopeg", kodeKopeg);
				// mav.addObject("type", typeUser);

				mav.setViewName(UPLOAD_KOPEG);
				return mav;
			} else {
				if (excelSvc.checkInListKoptelProduct(username, password)) {
					kodeKoptelProduct = username;
					typeUser = "koptelProduct";

					object.setUser(user);
					object.setTypeUser(typeUser);
					object.setKodeKopeg(kodeKopeg);
					object.setKodeKoptelProduct(kodeKoptelProduct);
					session.setAttribute("logindata", object);

					form = new MainAttribute();
					mav.addObject("form", form);

					// mav.addObject("user", user);
					// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
					// mav.addObject("type", typeUser);

					mav.setViewName(UPLOAD_KOPTEL_PRODUCT);
					return mav;
				} else {

					alert = "Failed to login. USERNAME AND PASSWORD IS NOT RECOGNIZED.";
					icon = "fail";

					mav.addObject("alert", alert);
					mav.addObject("icon", icon);

					mav.setViewName(BILLING);
					return mav;
				}
			}
		}

		alert = "Failed to login. USERNAME AND PASSWORD IS NOT RECOGNIZED.";
		icon = "fail";
		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		mav.setViewName(BILLING);
		return mav;
	}

	@RequestMapping("/request-logout")
	public ModelAndView requestLogout(ModelAndView mav, HttpServletRequest req) {

		object = null;
		session.setAttribute("logindata", object);
		session = null;
		
		mav.setViewName(BILLING);
		return mav;
	}

	@RequestMapping("/request-reset_password")
	public ModelAndView requestResetPassword(ModelAndView mav,
			HttpServletRequest req) {

		String username = req.getParameter("usernames");
		String alert = "";
		String icon = "";

		if (excelSvc.checkInListAdmin(username)) {
			excelSvc.resetPasswordAdmin(username);
		} else {
			if (excelSvc.checkInListKopeg(username)) {
				excelSvc.resetPasswordKopeg(username);
			} else {
				if (excelSvc.checkInListKoptelProduct(username)) {
					excelSvc.resetPasswordKoptelProduct(username);
				} else {

					alert = "Failed to reset password. USERNAME IS NOT RECOGNIZED.";
					icon = "fail";

					mav.addObject("alert", alert);
					mav.addObject("icon", icon);

					mav.setViewName(BILLING);
					return mav;
				}
			}
		}

		alert = "SUCCESS RESET PASSWORD. Your default password is 123456.";
		icon = "success";
		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		mav.setViewName(BILLING);
		return mav;
	}

	@RequestMapping("/request-change_password")
	public ModelAndView requestChangePassword(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();

		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String username = "";

		if (!object.getKodeKopeg().equals("")) {
			username = object.getKodeKopeg();
		} else {
			username = object.getKodeKoptelProduct();
		}

		String oldPassword = req.getParameter("old");
		String newPassword = req.getParameter("new");
		String confirmPassword = req.getParameter("confirm");
		String alert = "";
		String icon = "fail";

		if (!newPassword.equals(confirmPassword)) {
			alert = "Failed to change password. YOUR NEW PASSWORD DIFFERENCE WITH YOUR CONFIRM PASSWORD.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);

			// mav.addObject("user", user);
			// mav.addObject("kodeKopeg", kodeKopeg);
			// mav.addObject("type", typeUser);
			mav.setViewName(CHANGE_PASSWORD);
			return mav;
		}

		if (excelSvc.checkInListAdmin(username, oldPassword)) {
			excelSvc.changePasswordAdmin(username, newPassword);
		} else {
			if (excelSvc.checkInListKopeg(username, oldPassword)) {
				excelSvc.changePasswordKopeg(username, newPassword);
			} else {
				if (excelSvc.checkInListKoptelProduct(username, oldPassword)) {
					excelSvc.changePasswordKoptelProduct(username, newPassword);
				} else {

					alert = "Failed to change password. YOUR OLD PASSWORD IS NOT RECOGNIZED.";
					icon = "fail";
					mav.addObject("alert", alert);
					mav.addObject("icon", icon);

					// mav.addObject("user", user);
					// mav.addObject("kodeKopeg", kodeKopeg);
					// mav.addObject("type", typeUser);
					mav.setViewName(CHANGE_PASSWORD);
					return mav;
				}
			}
		}

		alert = "SUCCESS CHANGE PASSWORD. Your password has been changed.";
		icon = "success";
		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);
		mav.setViewName(CHANGE_PASSWORD);
		return mav;
	}

	// KOPEG
	@RequestMapping(value = "/request-upload_kopeg", headers = "content-type=multipart/*")
	public ModelAndView requestUploadKopeg(ModelAndView mav,
			@RequestParam(value = "report-file") MultipartFile file,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;
		int date = now.get(Calendar.DATE);

		String alert = "";
		String icon = "";

		String months = "";
		String years = "" + year;

		switch (month) {
		case 1:
			months = "January";
			break;
		case 2:
			months = "February";
			break;
		case 3:
			months = "March";
			break;
		case 4:
			months = "April";
			break;
		case 5:
			months = "May";
			break;
		case 6:
			months = "June";
			break;
		case 7:
			months = "July";
			break;
		case 8:
			months = "August";
			break;
		case 9:
			months = "September";
			break;
		case 10:
			months = "October";
			break;
		case 11:
			months = "November";
			break;
		case 12:
			months = "December";
			break;
		}

		String extension = "";

		if (file.getOriginalFilename().toLowerCase().endsWith("xlsx")) {
			extension = ".xlsx";
		} else if (file.getOriginalFilename().toLowerCase().endsWith("xls")) {
			extension = ".xls";
		}

		String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\Kopeg\\" + years
				+ "\\" + months;
		File files = new File(directory);
		if (!files.exists()) {
			if (files.mkdirs()) {
			}
		}
		String path = "C:/DIRECTORY_BILLING_MANAGEMENT/Kopeg/" + years + "/"
				+ months + "/" + object.getKodeKopeg() + extension;

		date = 1;

		if (date <= 12) {

			excelSvc.saveExcel(file, path);
			List<Kopeg> listKopeg = excelSvc.readKopeg(file, path,
					object.getKodeKopeg(), months, years, "");

			if (listKopeg == null) {
				alert = "Failed to upload. FORMAT DATA KOPEG IS NOT MATCH WITH DEFAULT FORMAT.";
				icon = "fail";
			} else {

				if (listKopeg.size() == 0) {
					alert = "Failed to upload. DATA KOPEG IS NOT MATCH WITH LOGIN KOPEG.";
					icon = "fail";
				} else {
					for (int i = 0; i < listKopeg.size(); i++) {
						if (listKopeg.get(i).getValidate().equals("false")) {
							alert = "Failed to upload. DATA EMPLOYEE IS INCORRECT.";
							icon = "fail";

							mav.addObject("listKopeg", listKopeg);
						}
					}
				}
			}

			if (alert.equals("")) {
				alert = "SUCCESS UPLOAD DATA KOPEG";
				icon = "success";

				List<Employee> listEmp = excelSvc
						.warningPensiunKopeg(listKopeg);

				mav.addObject("listKopeg", listKopeg);
				mav.addObject("listEmp", listEmp);
			}
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);
		} else {

			String type = "uploadKopeg";
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to upload. YOU MUST UPLOAD KOPEG BETWEEN 1ST DATE UNTIL 12ND DATE.";
			icon = "fail";

			mav.addObject("alert", alert);
			mav.addObject("icon", icon);
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_KOPEG);
		return mav;
	}

	@RequestMapping(value = "/request-upload_employee", headers = "content-type=multipart/*")
	public ModelAndView requestUploadEmployee(ModelAndView mav,
			@RequestParam(value = "report-file") MultipartFile file,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;

		String months = "";
		String years = "" + year;

		switch (month) {
		case 1:
			months = "January";
			break;
		case 2:
			months = "February";
			break;
		case 3:
			months = "March";
			break;
		case 4:
			months = "April";
			break;
		case 5:
			months = "May";
			break;
		case 6:
			months = "June";
			break;
		case 7:
			months = "July";
			break;
		case 8:
			months = "August";
			break;
		case 9:
			months = "September";
			break;
		case 10:
			months = "October";
			break;
		case 11:
			months = "November";
			break;
		case 12:
			months = "December";
			break;
		}

		String extension = "";

		if (file.getOriginalFilename().toLowerCase().endsWith("xlsx")) {
			extension = ".xlsx";
		} else if (file.getOriginalFilename().toLowerCase().endsWith("xls")) {
			extension = ".xls";
		}

		String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\Employee\\"
				+ years + "\\" + months;
		File files = new File(directory);
		if (!files.exists()) {
			if (files.mkdirs()) {
			}
		}
		String path = "C:/DIRECTORY_BILLING_MANAGEMENT/Employee/" + years + "/"
				+ months + "/" + object.getKodeKopeg() + extension;

		excelSvc.saveExcel(file, path);
		String alert = excelSvc.readEmployee(file, path, object.getKodeKopeg(),
				months, years, "");
		String icon = "";

		if (alert.equals("SUCCESS UPLOAD DATA EMPLOYEE")) {
			icon = "success";
		} else {
			if (alert
					.equals("Failed to upload. FORMAT DATA EMPLOYEE IS NOT MATCH WITH DEFAULT FORMAT.")) {
				icon = "fail";
			}
		}
		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_EMPLOYEE);
		return mav;
	}

	@RequestMapping(value = "/request-upload_list_kopeg", headers = "content-type=multipart/*")
	public ModelAndView requestUploadListKopeg(ModelAndView mav,
			@RequestParam(value = "report-file") MultipartFile file,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;

		String months = "";
		String years = "" + year;

		switch (month) {
		case 1:
			months = "January";
			break;
		case 2:
			months = "February";
			break;
		case 3:
			months = "March";
			break;
		case 4:
			months = "April";
			break;
		case 5:
			months = "May";
			break;
		case 6:
			months = "June";
			break;
		case 7:
			months = "July";
			break;
		case 8:
			months = "August";
			break;
		case 9:
			months = "September";
			break;
		case 10:
			months = "October";
			break;
		case 11:
			months = "November";
			break;
		case 12:
			months = "December";
			break;
		}

		String extension = "";

		if (file.getOriginalFilename().toLowerCase().endsWith("xlsx")) {
			extension = ".xlsx";
		} else if (file.getOriginalFilename().toLowerCase().endsWith("xls")) {
			extension = ".xls";
		}

		String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\ListKopeg\\"
				+ years + "\\" + months;
		File files = new File(directory);
		if (!files.exists()) {
			if (files.mkdirs()) {
			}
		}
		String path = "C:/DIRECTORY_BILLING_MANAGEMENT/ListKopeg/" + years
				+ "/" + months + "/" + object.getKodeKopeg() + extension;

		excelSvc.saveExcel(file, path);
		String alert = excelSvc.readListKopeg(file, path,
				object.getKodeKopeg(), months, years, "");
		String icon = "";

		if (alert.equals("SUCCESS UPLOAD DATA LIST KOPEG")) {
			icon = "success";
		} else {
			if (alert
					.equals("Failed to upload. FORMAT DATA LIST KOPEG IS NOT MATCH WITH DEFAULT FORMAT.")) {
				icon = "fail";
			}
		}
		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_LIST_KOPEG);
		return mav;
	}

	@RequestMapping("/request-report_kopeg")
	public ModelAndView requestReportKopeg(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String year = req.getParameter("year");

		if (month != null) {
			int mon = Integer.parseInt(month);
			String months = "";

			switch (mon) {
			case 1:
				months = "January";
				break;
			case 2:
				months = "February";
				break;
			case 3:
				months = "March";
				break;
			case 4:
				months = "April";
				break;
			case 5:
				months = "May";
				break;
			case 6:
				months = "June";
				break;
			case 7:
				months = "July";
				break;
			case 8:
				months = "August";
				break;
			case 9:
				months = "September";
				break;
			case 10:
				months = "October";
				break;
			case 11:
				months = "November";
				break;
			case 12:
				months = "December";
				break;
			}

			List<FileStatus> listFileStatus = excelSvc.findByDate(months, year);
			String alert = "NO FILE KOPEG UPLOAD";
			String icon = "fail";

			if (listFileStatus.isEmpty()) {
				mav.addObject("alert", alert);
				mav.addObject("icon", icon);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);
			} else {

				alert = "";
				icon = "";
				mav.addObject("report", listFileStatus);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);

				int totalKopeg = listKopegDao.getTotalKopeg();
				int success = 0;
				int failed = 0;
				String[] tempKodeKopeg = new String[totalKopeg];

				for (int i = 0; i < totalKopeg; i++) {
					tempKodeKopeg[i] = "";
				}

				for (int i = 0; i < listFileStatus.size(); i++) {
					if (listFileStatus.get(i).getStatus().equals("success")) {

						boolean s = true;
						for (int j = 0; j < totalKopeg; j++) {
							if (listFileStatus.get(i).getKodeKopeg()
									.equals(tempKodeKopeg[j])) {
								s = false;
							}
						}

						if (s == true) {
							tempKodeKopeg[success] = listFileStatus.get(i)
									.getKodeKopeg();
							success++;
						}

					}
				}

				failed = totalKopeg - success;

				mav.addObject("totalKopeg", totalKopeg);
				mav.addObject("success", success);
				mav.addObject("failed", failed);

			}
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(MERGE);
		return mav;
	}

	@RequestMapping("/request-merge")
	public ModelAndView requestMerge(ModelAndView mav, HttpServletRequest req,
			HttpServletResponse response) throws IOException {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String years = req.getParameter("year");

		Calendar now = Calendar.getInstance();
		int currentDate = now.get(Calendar.DATE);

		int mon = Integer.parseInt(month);
		String months = "";
		String alert = "";
		String icon = "";
		String path = "";

		currentDate = 13;

		if (currentDate >= 13) {

			if (month != null) {

				switch (mon) {
				case 1:
					months = "January";
					break;
				case 2:
					months = "February";
					break;
				case 3:
					months = "March";
					break;
				case 4:
					months = "April";
					break;
				case 5:
					months = "May";
					break;
				case 6:
					months = "June";
					break;
				case 7:
					months = "July";
					break;
				case 8:
					months = "August";
					break;
				case 9:
					months = "September";
					break;
				case 10:
					months = "October";
					break;
				case 11:
					months = "November";
					break;
				case 12:
					months = "December";
					break;
				}

				String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\Kopeg\\"
						+ years + "\\" + months;
				File files = new File(directory);
				if (!files.exists()) {
					if (files.mkdirs()) {
					}
				}
				path = "C:/DIRECTORY_BILLING_MANAGEMENT/Kopeg/" + years + "/"
						+ months + "/";

				alert = excelSvc.merge(years, months, path,
						object.getKodeKopeg(), "");

				if (alert.equals("SUCCESS SUMMARIZE DATA KOPEG")) {

					icon = "success";

					String file = "C:/DIRECTORY_BILLING_MANAGEMENT/Merge/"
							+ years + "/" + months + "-Merge.zip";

					File f = new File(file);
					FileInputStream fis = new FileInputStream(file);

					response.setContentType("application/xlsx");
					response.setContentLength(new Long(f.length()).intValue());

					String headerKey = "Content-Disposition";
					String headerValue = String.format(
							"attachment; filename=\"%s\"", f.getName());
					response.setHeader(headerKey, headerValue);

					OutputStream outStream = response.getOutputStream();

					byte[] buffer = new byte[4096];
					int bytesRead = -1;

					while ((bytesRead = fis.read(buffer)) != -1) {
						outStream.write(buffer, 0, bytesRead);
					}

					fis.close();
					outStream.close();

					mav.addObject("alert", alert);
					mav.addObject("icon", icon);

				} else {
					icon = "fail";
					mav.addObject("alert", alert);
					mav.addObject("icon", icon);
				}
			}
		} else {

			String type = "merge";
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to summarize. YOU MUST MERGE ABOVE 12ND DATE.";
			icon = "fail";

			mav.addObject("alert", alert);
			mav.addObject("icon", icon);
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(MERGE);
		return mav;
	}

	@RequestMapping(value = "/request-upload_compare", headers = "content-type=multipart/*")
	public ModelAndView requestUploadCompare(ModelAndView mav,
			@RequestParam(value = "report-file") MultipartFile file,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;
		int currentDate = now.get(Calendar.DATE);

		String months = "";
		String years = "" + year;

		String alert = "";
		String icon = "";
		String path = "";

		switch (month) {
		case 1:
			months = "January";
			break;
		case 2:
			months = "February";
			break;
		case 3:
			months = "March";
			break;
		case 4:
			months = "April";
			break;
		case 5:
			months = "May";
			break;
		case 6:
			months = "June";
			break;
		case 7:
			months = "July";
			break;
		case 8:
			months = "August";
			break;
		case 9:
			months = "September";
			break;
		case 10:
			months = "October";
			break;
		case 11:
			months = "November";
			break;
		case 12:
			months = "December";
			break;
		}

		String extension = "";

		if (file.getOriginalFilename().toLowerCase().endsWith("xlsx")) {
			extension = ".xlsx";
		} else if (file.getOriginalFilename().toLowerCase().endsWith("xls")) {
			extension = ".xls";
		}

		String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\Compare\\"
				+ years + "\\" + months;

		File files = new File(directory);
		if (!files.exists()) {
			if (files.mkdirs()) {
			}
		}

		String compare = "";
		String validation = "";

		if (file.getOriginalFilename().startsWith("CompareSimpananTetap")) {
			compare = "CompareSimpananTetap";

			path = "C:/DIRECTORY_BILLING_MANAGEMENT/Compare/" + years + "/"
					+ months + "/" + compare + extension;
			excelSvc.saveExcel(file, path);

			validation = excelSvc.validationCompareSimpananTetap(path);
		} else {
			if (file.getOriginalFilename().startsWith("CompareAngsuranTetap")) {
				compare = "CompareAngsuranTetap";

				path = "C:/DIRECTORY_BILLING_MANAGEMENT/Compare/" + years + "/"
						+ months + "/" + compare + extension;
				excelSvc.saveExcel(file, path);

				validation = excelSvc.validationCompareAngsuranTetap(path);
			} else {
				if (file.getOriginalFilename().startsWith(
						"CompareAngsuranLainLain")) {
					compare = "CompareAngsuranLainLain";

					path = "C:/DIRECTORY_BILLING_MANAGEMENT/Compare/" + years
							+ "/" + months + "/" + compare + extension;
					excelSvc.saveExcel(file, path);

					validation = excelSvc
							.validationCompareAngsuranLainLain(path);
				} else {

					String type = "uploadCompare";
					String status = "failed";
					Date dates = new Date();
					path = "-";

					FileStatus fileStatus = new FileStatus(
							object.getKodeKopeg(), type, months, years, status,
							path, "", dates);
					fileStatusDao.saveOrUpdate(fileStatus);

					alert = "Failed to upload. FORMAT FILENAME INCORRECT.";
					icon = "fail";
					mav.addObject("alert", alert);
					mav.addObject("icon", icon);

					// mav.addObject("user", user);
					// mav.addObject("kodeKopeg", kodeKopeg);
					// mav.addObject("type", typeUser);

					mav.setViewName(UPLOAD_COMPARE);
					return mav;
				}
			}
		}

		if (validation.equals("false")) {

			String type = "upload" + compare;
			String status = "failed";
			Date dates = new Date();

			File filed = new File(path);
			filed.delete();
			path = "-";

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to upload. FORMAT DATA COMPARE IS NOT MATCH WITH DEFAULT FORMAT.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);

			// mav.addObject("user", user);
			// mav.addObject("kodeKopeg", kodeKopeg);
			// mav.addObject("type", typeUser);

			mav.setViewName(UPLOAD_COMPARE);
			return mav;
		}

		currentDate = 13;

		if (currentDate >= 13) {

			String type = "upload" + compare;
			String status = "success";
			Date date = new Date();

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", date);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "UPLOAD " + compare + " SUCCESS";
			icon = "success";
		} else {

			String type = "upload" + compare;
			String status = "failed";
			Date dates = new Date();

			File filed = new File(path);
			filed.delete();
			path = "-";

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to upload. YOU MUST UPLOAD COMPARE ABOVE 12ND DATE.";
			icon = "fail";
		}

		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_COMPARE);
		return mav;
	}

	@RequestMapping("/request-report_compare")
	public ModelAndView requestReportCompare(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String year = req.getParameter("year");

		if (month != null) {
			int mon = Integer.parseInt(month);
			String months = "";

			switch (mon) {
			case 1:
				months = "January";
				break;
			case 2:
				months = "February";
				break;
			case 3:
				months = "March";
				break;
			case 4:
				months = "April";
				break;
			case 5:
				months = "May";
				break;
			case 6:
				months = "June";
				break;
			case 7:
				months = "July";
				break;
			case 8:
				months = "August";
				break;
			case 9:
				months = "September";
				break;
			case 10:
				months = "October";
				break;
			case 11:
				months = "November";
				break;
			case 12:
				months = "December";
				break;
			}

			List<FileStatus> listFileStatus = excelSvc
					.findCompare(months, year);
			String alert = "NO FILE COMPARE UPLOAD";
			String icon = "fail";

			if (listFileStatus.isEmpty()) {
				mav.addObject("alert", alert);
				mav.addObject("icon", icon);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);
			} else {

				alert = "";
				icon = "";
				mav.addObject("report", listFileStatus);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);

				String simpananTetap = "failed / not yet";
				String angsuranTetap = "failed / not yet";
				String angsuranLainLain = "failed / not yet";
				String completed = "failed";

				for (int i = 0; i < listFileStatus.size(); i++) {
					if (listFileStatus.get(i).getStatus().equals("success")) {

						if (listFileStatus.get(i).getType()
								.equals("uploadCompareSimpananTetap")) {
							simpananTetap = "success";
						}
						if (listFileStatus.get(i).getType()
								.equals("uploadCompareAngsuranTetap")) {
							angsuranTetap = "success";
						}
						if (listFileStatus.get(i).getType()
								.equals("uploadCompareAngsuranLainLain")) {
							angsuranLainLain = "success";
						}
					}
				}

				if ((simpananTetap.equals("success"))
						&& (angsuranTetap.equals("success"))
						&& (angsuranLainLain.equals("success"))) {
					completed = "success";
				}

				mav.addObject("simpananTetap", simpananTetap);
				mav.addObject("angsuranTetap", angsuranTetap);
				mav.addObject("angsuranLainLain", angsuranLainLain);
				mav.addObject("completed", completed);
			}
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(COMPARE);
		return mav;
	}

	@RequestMapping("/request-compare")
	public ModelAndView requestCompare(ModelAndView mav,
			HttpServletRequest req, HttpServletResponse response)
			throws IOException {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String years = req.getParameter("year");

		Calendar now = Calendar.getInstance();
		int currentDate = now.get(Calendar.DATE);

		int mon = Integer.parseInt(month);
		String months = "";
		String alert = "";
		String icon = "";
		String path = "";

		currentDate = 13;

		if (currentDate >= 13) {

			if (month != null) {

				switch (mon) {
				case 1:
					months = "January";
					break;
				case 2:
					months = "February";
					break;
				case 3:
					months = "March";
					break;
				case 4:
					months = "April";
					break;
				case 5:
					months = "May";
					break;
				case 6:
					months = "June";
					break;
				case 7:
					months = "July";
					break;
				case 8:
					months = "August";
					break;
				case 9:
					months = "September";
					break;
				case 10:
					months = "October";
					break;
				case 11:
					months = "November";
					break;
				case 12:
					months = "December";
					break;
				}

				String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\Compare\\"
						+ years + "\\" + months;

				File files = new File(directory);
				if (!files.exists()) {
					if (files.mkdirs()) {
					}
				}

				path = "C:/DIRECTORY_BILLING_MANAGEMENT/Compare/" + years + "/"
						+ months + "/";

				alert = excelSvc.getDifference(years, months, path,
						object.getKodeKopeg(), "");

				if (alert.equals("SUCCESS COMPARE DATA")) {

					icon = "success";

					String file = "C:\\DIRECTORY_BILLING_MANAGEMENT\\Compare\\"
							+ years + "\\" + months + "-Difference.xlsx";

					File f = new File(file);
					FileInputStream fis = new FileInputStream(file);

					response.setContentType("application/xlsx");
					response.setContentLength(new Long(f.length()).intValue());

					String headerKey = "Content-Disposition";
					String headerValue = String.format(
							"attachment; filename=\"%s\"", f.getName());
					response.setHeader(headerKey, headerValue);

					OutputStream outStream = response.getOutputStream();

					byte[] buffer = new byte[4096];
					int bytesRead = -1;

					while ((bytesRead = fis.read(buffer)) != -1) {
						outStream.write(buffer, 0, bytesRead);
					}

					fis.close();
					outStream.close();

					mav.addObject("alert", alert);
					mav.addObject("icon", icon);
				} else {
					icon = "fail";
					mav.addObject("alert", alert);
					mav.addObject("icon", icon);
				}
			}
		} else {

			String type = "compare";
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to compare. YOU MUST COMPARE ABOVE 12ND DATE.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(COMPARE);
		return mav;
	}

	@RequestMapping(value = "/request-upload_final_report", headers = "content-type=multipart/*")
	public ModelAndView requestUploadFinalReport(ModelAndView mav,
			@RequestParam(value = "report-file") MultipartFile file,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;
		int currentDate = now.get(Calendar.DATE);

		String months = "";
		String years = "" + year;

		String alert = "";
		String icon = "";
		String path = "";

		switch (month) {
		case 1:
			months = "January";
			break;
		case 2:
			months = "February";
			break;
		case 3:
			months = "March";
			break;
		case 4:
			months = "April";
			break;
		case 5:
			months = "May";
			break;
		case 6:
			months = "June";
			break;
		case 7:
			months = "July";
			break;
		case 8:
			months = "August";
			break;
		case 9:
			months = "September";
			break;
		case 10:
			months = "October";
			break;
		case 11:
			months = "November";
			break;
		case 12:
			months = "December";
			break;
		}

		String extension = "";

		if (file.getOriginalFilename().toLowerCase().endsWith("xlsx")) {
			extension = ".xlsx";
		} else if (file.getOriginalFilename().toLowerCase().endsWith("xls")) {
			extension = ".xls";
		}

		String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\FinalReport\\"
				+ years + "\\" + months;

		File files = new File(directory);
		if (!files.exists()) {
			if (files.mkdirs()) {
			}
		}

		String report = "";
		String validation = "";

		if (file.getOriginalFilename().startsWith("ReportSimpananTetap")) {
			report = "ReportSimpananTetap";

			path = "C:/DIRECTORY_BILLING_MANAGEMENT/FinalReport/" + years + "/"
					+ months + "/" + report + extension;
			excelSvc.saveExcel(file, path);

			validation = excelSvc.validationReportSimpananTetap(path);
		} else {
			if (file.getOriginalFilename().startsWith("ReportAngsuranTetap")) {
				report = "ReportAngsuranTetap";

				path = "C:/DIRECTORY_BILLING_MANAGEMENT/FinalReport/" + years
						+ "/" + months + "/" + report + extension;
				excelSvc.saveExcel(file, path);

				validation = excelSvc.validationReportAngsuranTetap(path);
			} else {
				if (file.getOriginalFilename().startsWith(
						"ReportAngsuranLainLain")) {
					report = "ReportAngsuranLainLain";

					path = "C:/DIRECTORY_BILLING_MANAGEMENT/FinalReport/"
							+ years + "/" + months + "/" + report + extension;
					excelSvc.saveExcel(file, path);

					validation = excelSvc
							.validationReportAngsuranLainLain(path);
				} else {

					String type = "uploadFinalReport";
					String status = "failed";
					Date dates = new Date();
					path = "-";

					FileStatus fileStatus = new FileStatus(
							object.getKodeKopeg(), type, months, years, status,
							path, "", dates);
					fileStatusDao.saveOrUpdate(fileStatus);

					alert = "Failed to upload. FORMAT FILENAME INCORRECT.";
					icon = "fail";
					mav.addObject("alert", alert);
					mav.addObject("icon", icon);

					// mav.addObject("user", user);
					// mav.addObject("kodeKopeg", kodeKopeg);
					// mav.addObject("type", typeUser);

					mav.setViewName(UPLOAD_FINAL_REPORT);
					return mav;
				}
			}
		}

		if (validation.equals("false")) {

			String type = "uploadFinal" + report;
			String status = "failed";
			Date dates = new Date();

			File filed = new File(path);
			filed.delete();
			path = "-";

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to upload. FORMAT DATA FINAL REPORT IS NOT MATCH WITH DEFAULT FORMAT.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);

			// mav.addObject("user", user);
			// mav.addObject("kodeKopeg", kodeKopeg);
			// mav.addObject("type", typeUser);

			mav.setViewName(UPLOAD_FINAL_REPORT);
			return mav;
		}

		currentDate = 13;

		if (currentDate >= 13) {

			String type = "uploadFinal" + report;
			String status = "success";
			Date date = new Date();

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", date);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "UPLOAD FINAL " + report + " SUCCESS";
			icon = "success";
		} else {

			String type = "uploadFinal" + report;
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to upload. YOU MUST UPLOAD FINAL REPORT ABOVE 12ND DATE.";
			icon = "failed";
		}

		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_FINAL_REPORT);
		return mav;
	}

	@RequestMapping("/request-report_final_report")
	public ModelAndView requestReportFinalReport(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String year = req.getParameter("year");

		if (month != null) {
			int mon = Integer.parseInt(month);
			String months = "";

			switch (mon) {
			case 1:
				months = "January";
				break;
			case 2:
				months = "February";
				break;
			case 3:
				months = "March";
				break;
			case 4:
				months = "April";
				break;
			case 5:
				months = "May";
				break;
			case 6:
				months = "June";
				break;
			case 7:
				months = "July";
				break;
			case 8:
				months = "August";
				break;
			case 9:
				months = "September";
				break;
			case 10:
				months = "October";
				break;
			case 11:
				months = "November";
				break;
			case 12:
				months = "December";
				break;
			}

			List<FileStatus> listFileStatus = excelSvc.findFinalReport(months,
					year);
			String alert = "NO FILE FINAL REPORT UPLOAD";
			String icon = "fail";

			if (listFileStatus.isEmpty()) {
				mav.addObject("alert", alert);
				mav.addObject("icon", icon);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);
			} else {

				alert = "";
				icon = "";
				mav.addObject("report", listFileStatus);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);

				String simpananTetap = "failed / not yet";
				String angsuranTetap = "failed / not yet";
				String angsuranLainLain = "failed / not yet";
				String completed = "failed";

				for (int i = 0; i < listFileStatus.size(); i++) {
					if (listFileStatus.get(i).getStatus().equals("success")) {

						if (listFileStatus.get(i).getType()
								.equals("uploadFinalReportSimpananTetap")) {
							simpananTetap = "success";
						}
						if (listFileStatus.get(i).getType()
								.equals("uploadFinalReportAngsuranTetap")) {
							angsuranTetap = "success";
						}
						if (listFileStatus.get(i).getType()
								.equals("uploadFinalReportAngsuranLainLain")) {
							angsuranLainLain = "success";
						}
					}
				}

				if ((simpananTetap.equals("success"))
						&& (angsuranTetap.equals("success"))
						&& (angsuranLainLain.equals("success"))) {
					completed = "success";
				}

				mav.addObject("simpananTetap", simpananTetap);
				mav.addObject("angsuranTetap", angsuranTetap);
				mav.addObject("angsuranLainLain", angsuranLainLain);
				mav.addObject("completed", completed);
			}
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(SPLIT);
		return mav;
	}

	@RequestMapping("/request-split")
	public ModelAndView requestSplit(ModelAndView mav, HttpServletRequest req,
			HttpServletResponse response) throws IOException {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String years = req.getParameter("year");

		Calendar now = Calendar.getInstance();
		int currentDate = now.get(Calendar.DATE);

		int mon = Integer.parseInt(month);
		String months = "";
		String alert = "";
		String icon = "";
		String path = "";

		currentDate = 13;

		if (currentDate >= 13) {

			if (month != null) {

				switch (mon) {
				case 1:
					months = "January";
					break;
				case 2:
					months = "February";
					break;
				case 3:
					months = "March";
					break;
				case 4:
					months = "April";
					break;
				case 5:
					months = "May";
					break;
				case 6:
					months = "June";
					break;
				case 7:
					months = "July";
					break;
				case 8:
					months = "August";
					break;
				case 9:
					months = "September";
					break;
				case 10:
					months = "October";
					break;
				case 11:
					months = "November";
					break;
				case 12:
					months = "December";
					break;
				}

				String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\FinalReport\\"
						+ years + "\\" + months;

				File files = new File(directory);
				if (!files.exists()) {
					if (files.mkdirs()) {
					}
				}
				path = "C:/DIRECTORY_BILLING_MANAGEMENT/FinalReport/" + years
						+ "/" + months + "/";

				alert = excelSvc.getSplitFinalReport(years, months, path,
						object.getKodeKopeg(), "");

				icon = "";

				if (alert.equals("SUCCESS SPLIT FINAL REPORT")) {
					icon = "success";
				} else {
					if (alert
							.equals("Failed to split. FILE FINAL REPORT IS NOT FOUND IN DIRECTORY.")) {
						icon = "fail";
					}
				}

				mav.addObject("alert", alert);
				mav.addObject("icon", icon);
			}
		} else {

			String type = "split";
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to split. YOU MUST SPLIT ABOVE 12ND DATE.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(SPLIT);
		return mav;
	}

	@RequestMapping("/request-download_final_report")
	public ModelAndView requestDownloadFinalReport(ModelAndView mav,
			HttpServletRequest req, HttpServletResponse response)
			throws IOException {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String years = req.getParameter("year");

		Calendar now = Calendar.getInstance();
		int currentDate = now.get(Calendar.DATE);

		int mon = Integer.parseInt(month);
		String months = "";
		String alert = "";
		String icon = "";
		String path = "";

		currentDate = 13;

		if (currentDate >= 13) {

			if (month != null) {

				switch (mon) {
				case 1:
					months = "January";
					break;
				case 2:
					months = "February";
					break;
				case 3:
					months = "March";
					break;
				case 4:
					months = "April";
					break;
				case 5:
					months = "May";
					break;
				case 6:
					months = "June";
					break;
				case 7:
					months = "July";
					break;
				case 8:
					months = "August";
					break;
				case 9:
					months = "September";
					break;
				case 10:
					months = "October";
					break;
				case 11:
					months = "November";
					break;
				case 12:
					months = "December";
					break;
				}

				String file = "C:/DIRECTORY_BILLING_MANAGEMENT/SplitFinalReport/"
						+ years
						+ "/"
						+ months
						+ "/"
						+ object.getKodeKopeg()
						+ "-FinalReport.xlsx";

				File f;
				FileInputStream fis;

				try {

					f = new File(file);
					fis = new FileInputStream(file);

					response.setContentType("application/xlsx");
					response.setContentLength(new Long(f.length()).intValue());

					String headerKey = "Content-Disposition";
					String headerValue = String.format(
							"attachment; filename=\"%s\"", f.getName());
					response.setHeader(headerKey, headerValue);

					OutputStream outStream = response.getOutputStream();

					byte[] buffer = new byte[4096];
					int bytesRead = -1;

					while ((bytesRead = fis.read(buffer)) != -1) {
						outStream.write(buffer, 0, bytesRead);
					}

					fis.close();
					outStream.close();

					String type = "downloadFinalReport";
					String status = "success";
					Date dates = new Date();

					FileStatus fileStatus = new FileStatus(
							object.getKodeKopeg(), type, months, years, status,
							file, "", dates);
					fileStatusDao.saveOrUpdate(fileStatus);

					alert = "SUCCESS DOWNLOAD FINAL REPORT";
					icon = "success";

				} catch (Exception e) {

					String type = "downloadFinalReport";
					String status = "failed";
					Date dates = new Date();
					path = "-";

					FileStatus fileStatus = new FileStatus(
							object.getKodeKopeg(), type, months, years, status,
							path, "", dates);
					fileStatusDao.saveOrUpdate(fileStatus);

					System.out
							.println("Failed to download. FINAL REPORT IS NOT FOUND IN DIRECTORY. PLEASE SPLIT BY ADMIN.");

					alert = "Failed to download. FINAL REPORT IS NOT FOUND IN DIRECTORY. PLEASE SPLIT BY ADMIN.";
					icon = "fail";
				}
			}
		} else {

			String type = "downloadFinalReport";
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(object.getKodeKopeg(), type,
					months, years, status, path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to download. YOU MUST DOWNLOAD ABOVE 12ND DATE.";
			icon = "fail";
		}

		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		session.setAttribute("logindata", object);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(DOWNLOAD_FINAL_REPORT);
		return mav;
	}

	// KOPTEL PRODUCT
	@RequestMapping(value = "/request-upload_koptel_product", headers = "content-type=multipart/*")
	public ModelAndView requestUploadKoptelProduct(ModelAndView mav,
			@RequestParam(value = "report-file") MultipartFile file,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;
		int date = now.get(Calendar.DATE);

		String alert = "";
		String icon = "";

		String months = "";
		String years = "" + year;

		switch (month) {
		case 1:
			months = "January";
			break;
		case 2:
			months = "February";
			break;
		case 3:
			months = "March";
			break;
		case 4:
			months = "April";
			break;
		case 5:
			months = "May";
			break;
		case 6:
			months = "June";
			break;
		case 7:
			months = "July";
			break;
		case 8:
			months = "August";
			break;
		case 9:
			months = "September";
			break;
		case 10:
			months = "October";
			break;
		case 11:
			months = "November";
			break;
		case 12:
			months = "December";
			break;
		}

		String extension = "";

		if (file.getOriginalFilename().toLowerCase().endsWith("xlsx")) {
			extension = ".xlsx";
		} else if (file.getOriginalFilename().toLowerCase().endsWith("xls")) {
			extension = ".xls";
		}

		String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\KoptelProduct\\"
				+ years + "\\" + months;
		File files = new File(directory);
		if (!files.exists()) {
			if (files.mkdirs()) {
			}
		}

		String path = "";
		String product = "";

		if (file.getOriginalFilename().startsWith("AngsuranTetap")) {
			product = "AngsuranTetap";

			path = "C:/DIRECTORY_BILLING_MANAGEMENT/KoptelProduct/" + years
					+ "/" + months + "/" + product + "-"
					+ object.getKodeKoptelProduct() + extension;

		} else {
			if (file.getOriginalFilename().startsWith("AngsuranLainLain")) {
				product = "AngsuranLainLain";

				path = "C:/DIRECTORY_BILLING_MANAGEMENT/KoptelProduct/" + years
						+ "/" + months + "/" + product + "-"
						+ object.getKodeKoptelProduct() + extension;

			} else {

				String type = "uploadAngsuran";
				String status = "failed";
				Date dates = new Date();
				path = "-";

				FileStatus fileStatus = new FileStatus(
						object.getKodeKoptelProduct(), type, months, years,
						status, path, "", dates);
				fileStatusDao.saveOrUpdate(fileStatus);

				alert = "Failed to upload. FORMAT FILENAME INCORRECT.";
				icon = "fail";
				mav.addObject("alert", alert);
				mav.addObject("icon", icon);

				// mav.addObject("user", user);
				// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
				// mav.addObject("type", typeUser);

				mav.setViewName(UPLOAD_KOPTEL_PRODUCT);
				return mav;
			}
		}

		date = 1;

		if (date <= 12) {

			excelSvc.saveExcel(file, path);
			List<KoptelProduct> listKoptelProduct = excelSvc.readKoptelProduct(
					file, path, object.getKodeKoptelProduct(), months, years,
					"", product);

			if (listKoptelProduct == null) {
				alert = "Failed to upload. FORMAT DATA KOPTEL PRODUCT IS NOT MATCH WITH DEFAULT FORMAT.";
				icon = "fail";
			} else {

				if (listKoptelProduct.size() == 0) {
					alert = "Failed to upload. DATA KOPTEL PRODUCT IS NOT MATCH WITH LOGIN KOPTEL PRODUCT.";
					icon = "fail";
				} else {
					for (int i = 0; i < listKoptelProduct.size(); i++) {
						if (listKoptelProduct.get(i).getValidate()
								.equals("false")) {
							alert = "Failed to upload. DATA EMPLOYEE IS INCORRECT.";
							icon = "fail";

							mav.addObject("listKoptelProduct",
									listKoptelProduct);
						}
					}
				}
			}

			if (alert.equals("")) {
				alert = "SUCCESS UPLOAD DATA KOPTEL PRODUCT";
				icon = "success";

				List<Employee> listEmp = excelSvc
						.warningPensiunKoptelProduct(listKoptelProduct);

				mav.addObject("listEmp", listEmp);
				mav.addObject("listKoptelProduct", listKoptelProduct);
			}
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);
		} else {

			String type = "upload" + product;
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to upload. YOU MUST UPLOAD KOPTEL PRODUCT BETWEEN 1ST DATE UNTIL 12ND DATE.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_KOPTEL_PRODUCT);
		return mav;
	}

	@RequestMapping(value = "/request-upload_list_koptel_product", headers = "content-type=multipart/*")
	public ModelAndView requestUploadListKoptelProduct(ModelAndView mav,
			@RequestParam(value = "report-file") MultipartFile file,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;

		String months = "";
		String years = "" + year;

		switch (month) {
		case 1:
			months = "January";
			break;
		case 2:
			months = "February";
			break;
		case 3:
			months = "March";
			break;
		case 4:
			months = "April";
			break;
		case 5:
			months = "May";
			break;
		case 6:
			months = "June";
			break;
		case 7:
			months = "July";
			break;
		case 8:
			months = "August";
			break;
		case 9:
			months = "September";
			break;
		case 10:
			months = "October";
			break;
		case 11:
			months = "November";
			break;
		case 12:
			months = "December";
			break;
		}

		String extension = "";

		if (file.getOriginalFilename().toLowerCase().endsWith("xlsx")) {
			extension = ".xlsx";
		} else if (file.getOriginalFilename().toLowerCase().endsWith("xls")) {
			extension = ".xls";
		}

		String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\ListKoptelProduct\\"
				+ years + "\\" + months;
		File files = new File(directory);
		if (!files.exists()) {
			if (files.mkdirs()) {
			}
		}
		String path = "C:/DIRECTORY_BILLING_MANAGEMENT/ListKoptelProduct/"
				+ years + "/" + months + "/" + object.getKodeKopeg()
				+ extension;

		excelSvc.saveExcel(file, path);
		String alert = excelSvc.readListKoptelProduct(file, path,
				object.getKodeKopeg(), months, years, "");
		String icon = "";

		if (alert.equals("SUCCESS UPLOAD DATA LIST KOPTEL PRODUCT")) {
			icon = "success";
		} else {
			if (alert
					.equals("Failed to upload. FORMAT DATA LIST KOPTEL PRODUCT IS NOT MATCH WITH DEFAULT FORMAT.")) {
				icon = "fail";
			}
		}
		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_LIST_KOPTEL_PRODUCT);
		return mav;
	}

	@RequestMapping("/request-report_koptel_product")
	public ModelAndView requestReportKoptelProduct(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String year = req.getParameter("year");

		if (month != null) {
			int mon = Integer.parseInt(month);
			String months = "";

			switch (mon) {
			case 1:
				months = "January";
				break;
			case 2:
				months = "February";
				break;
			case 3:
				months = "March";
				break;
			case 4:
				months = "April";
				break;
			case 5:
				months = "May";
				break;
			case 6:
				months = "June";
				break;
			case 7:
				months = "July";
				break;
			case 8:
				months = "August";
				break;
			case 9:
				months = "September";
				break;
			case 10:
				months = "October";
				break;
			case 11:
				months = "November";
				break;
			case 12:
				months = "December";
				break;
			}

			List<FileStatus> listFileStatus = excelSvc.findByDateProduct(
					months, year);
			String alert = "NO FILE KOPTEL PRODUCT UPLOAD";
			String icon = "fail";

			if (listFileStatus.isEmpty()) {
				mav.addObject("alert", alert);
				mav.addObject("icon", icon);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);
			} else {

				alert = "";
				icon = "";
				mav.addObject("report", listFileStatus);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);

				int totalKoptelProduct = listKoptelProductDao
						.getTotalKoptelProduct();
				int success = 0;
				int failed = 0;

				int temp = totalKoptelProduct * 2;
				String[] tempKodeKoptelProductType = new String[temp];
				String[] tempKodeKoptelProduct = new String[totalKoptelProduct];

				for (int i = 0; i < temp; i++) {
					tempKodeKoptelProductType[i] = "";
				}

				for (int i = 0; i < totalKoptelProduct; i++) {
					tempKodeKoptelProduct[i] = "";
				}

				int count = 0;
				for (int i = 0; i < listFileStatus.size(); i++) {
					if (listFileStatus.get(i).getStatus().equals("success")) {

						String tempKodePlusType = listFileStatus.get(i)
								.getKodeKopeg()
								+ listFileStatus.get(i).getType();

						boolean s = true;
						for (int j = 0; j < temp; j++) {

							if (tempKodePlusType
									.equals(tempKodeKoptelProductType[j])) {
								s = false;
							}
						}

						if (s == true) {
							tempKodeKoptelProductType[count] = tempKodePlusType;
							count++;
						}

					}
				}

				for (int i = 0; i < count; i++) {

					String kodeKoptelProduct = tempKodeKoptelProductType[i]
							.substring(0, 6);

					boolean s = true;
					for (int j = 0; j < totalKoptelProduct; j++) {

						if (kodeKoptelProduct.equals(tempKodeKoptelProduct[j])) {
							s = false;
						}
					}

					if (s == true) {

						int file = 0;
						// cek apakah 1 koptel product sudah upload 2 file belum
						// : angsuran tetap dan angsuran lain"
						for (int a = 0; a < count; a++) {

							String kode = tempKodeKoptelProductType[a]
									.substring(0, 6);

							if (kode.equals(kodeKoptelProduct)) {
								file++;
							}
						}

						if (file == 2) {
							tempKodeKoptelProduct[success] = kodeKoptelProduct;
							success++;
						}
					}
				}

				failed = totalKoptelProduct - success;

				mav.addObject("totalKopeg", totalKoptelProduct);
				mav.addObject("success", success);
				mav.addObject("failed", failed);

			}
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(MERGE_PRODUCT);
		return mav;
	}

	@RequestMapping("/request-merge_product")
	public ModelAndView requestMergeProduct(ModelAndView mav,
			HttpServletRequest req, HttpServletResponse response)
			throws IOException {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String years = req.getParameter("year");

		Calendar now = Calendar.getInstance();
		int currentDate = now.get(Calendar.DATE);

		int mon = Integer.parseInt(month);
		String months = "";
		String alert = "";
		String icon = "";
		String path = "";

		currentDate = 13;

		if (currentDate >= 13) {

			if (month != null) {

				switch (mon) {
				case 1:
					months = "January";
					break;
				case 2:
					months = "February";
					break;
				case 3:
					months = "March";
					break;
				case 4:
					months = "April";
					break;
				case 5:
					months = "May";
					break;
				case 6:
					months = "June";
					break;
				case 7:
					months = "July";
					break;
				case 8:
					months = "August";
					break;
				case 9:
					months = "September";
					break;
				case 10:
					months = "October";
					break;
				case 11:
					months = "November";
					break;
				case 12:
					months = "December";
					break;
				}

				String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\KoptelProduct\\"
						+ years + "\\" + months;
				File files = new File(directory);
				if (!files.exists()) {
					if (files.mkdirs()) {
					}
				}
				path = "C:/DIRECTORY_BILLING_MANAGEMENT/KoptelProduct/" + years
						+ "/" + months + "/";

				alert = excelSvc.mergeProduct(years, months, path,
						object.getKodeKoptelProduct(), "");

				if (alert.equals("SUCCESS SUMMARIZE DATA KOPTEL PRODUCT")) {

					icon = "success";

					String file = "C:\\DIRECTORY_BILLING_MANAGEMENT\\MergeProduct\\"
							+ years + "\\" + months + "-MergeProduct.zip";

					File f = new File(file);
					FileInputStream fis = new FileInputStream(file);

					response.setContentType("application/xlsx");
					response.setContentLength(new Long(f.length()).intValue());

					String headerKey = "Content-Disposition";
					String headerValue = String.format(
							"attachment; filename=\"%s\"", f.getName());
					response.setHeader(headerKey, headerValue);

					OutputStream outStream = response.getOutputStream();

					byte[] buffer = new byte[4096];
					int bytesRead = -1;

					while ((bytesRead = fis.read(buffer)) != -1) {
						outStream.write(buffer, 0, bytesRead);
					}

					fis.close();
					outStream.close();

					mav.addObject("alert", alert);
					mav.addObject("icon", icon);

				} else {
					icon = "fail";
					mav.addObject("alert", alert);
					mav.addObject("icon", icon);
				}
			}
		} else {

			String type = "mergeProduct";
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to summarize. YOU MUST MERGE PRODUCT ABOVE 12ND DATE.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(MERGE_PRODUCT);
		return mav;
	}

	@RequestMapping(value = "/request-upload_compare_product", headers = "content-type=multipart/*")
	public ModelAndView requestUploadCompareProduct(ModelAndView mav,

	@RequestParam(value = "report-file") MultipartFile file,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;
		int currentDate = now.get(Calendar.DATE);

		String months = "";
		String years = "" + year;

		String alert = "";
		String icon = "";
		String path = "";

		switch (month) {
		case 1:
			months = "January";
			break;
		case 2:
			months = "February";
			break;
		case 3:
			months = "March";
			break;
		case 4:
			months = "April";
			break;
		case 5:
			months = "May";
			break;
		case 6:
			months = "June";
			break;
		case 7:
			months = "July";
			break;
		case 8:
			months = "August";
			break;
		case 9:
			months = "September";
			break;
		case 10:
			months = "October";
			break;
		case 11:
			months = "November";
			break;
		case 12:
			months = "December";
			break;
		}

		String extension = "";

		if (file.getOriginalFilename().toLowerCase().endsWith("xlsx")) {
			extension = ".xlsx";
		} else if (file.getOriginalFilename().toLowerCase().endsWith("xls")) {
			extension = ".xls";
		}

		String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\CompareProduct\\"
				+ years + "\\" + months;

		File files = new File(directory);
		if (!files.exists()) {
			if (files.mkdirs()) {
			}
		}

		String compare = "";
		String validation = "";

		if (file.getOriginalFilename()
				.startsWith("CompareAngsuranTetapProduct")) {
			compare = "CompareAngsuranTetapProduct";

			path = "C:/DIRECTORY_BILLING_MANAGEMENT/CompareProduct/" + years
					+ "/" + months + "/" + compare + extension;
			excelSvc.saveExcel(file, path);

			validation = excelSvc.validationCompareAngsuranTetapProduct(path);
		} else {
			if (file.getOriginalFilename().startsWith(
					"CompareAngsuranLainLainProduct")) {
				compare = "CompareAngsuranLainLainProduct";

				path = "C:/DIRECTORY_BILLING_MANAGEMENT/CompareProduct/"
						+ years + "/" + months + "/" + compare + extension;
				excelSvc.saveExcel(file, path);

				validation = excelSvc
						.validationCompareAngsuranLainLainProduct(path);
			} else {

				String type = "uploadCompareProduct";
				String status = "failed";
				Date dates = new Date();
				path = "-";

				FileStatus fileStatus = new FileStatus(
						object.getKodeKoptelProduct(), type, months, years,
						status, path, "", dates);
				fileStatusDao.saveOrUpdate(fileStatus);

				alert = "Failed to upload. FORMAT FILENAME INCORRECT.";
				icon = "fail";
				mav.addObject("alert", alert);
				mav.addObject("icon", icon);

				// mav.addObject("user", user);
				// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
				// mav.addObject("type", typeUser);

				mav.setViewName(UPLOAD_COMPARE_PRODUCT);
				return mav;
			}
		}

		if (validation.equals("false")) {

			String type = "upload" + compare;
			String status = "failed";
			Date dates = new Date();

			File filed = new File(path);
			filed.delete();
			path = "-";

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to upload. FORMAT DATA COMPARE PRODUCT IS NOT MATCH WITH DEFAULT FORMAT.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);

			// mav.addObject("user", user);
			// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
			// mav.addObject("type", typeUser);

			mav.setViewName(UPLOAD_COMPARE_PRODUCT);
			return mav;
		}

		currentDate = 13;

		if (currentDate >= 13) {

			String type = "upload" + compare;
			String status = "success";
			Date date = new Date();

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", date);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "UPLOAD " + compare + " SUCCESS";
			icon = "success";
		} else {

			String type = "upload" + compare;
			String status = "failed";
			Date dates = new Date();

			File filed = new File(path);
			filed.delete();
			path = "-";

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to upload. YOU MUST UPLOAD COMPARE PRODUCT ABOVE 12ND DATE.";
			icon = "fail";
		}

		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		// mav.addObject("user", user);
		// mav.addObject("kodeKopeg", kodeKopeg);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_COMPARE_PRODUCT);
		return mav;
	}

	@RequestMapping("/request-report_compare_product")
	public ModelAndView requestReportCompareProduct(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String year = req.getParameter("year");

		if (month != null) {
			int mon = Integer.parseInt(month);
			String months = "";

			switch (mon) {
			case 1:
				months = "January";
				break;
			case 2:
				months = "February";
				break;
			case 3:
				months = "March";
				break;
			case 4:
				months = "April";
				break;
			case 5:
				months = "May";
				break;
			case 6:
				months = "June";
				break;
			case 7:
				months = "July";
				break;
			case 8:
				months = "August";
				break;
			case 9:
				months = "September";
				break;
			case 10:
				months = "October";
				break;
			case 11:
				months = "November";
				break;
			case 12:
				months = "December";
				break;
			}

			List<FileStatus> listFileStatus = excelSvc.findCompareProduct(
					months, year);
			String alert = "NO FILE COMPARE PRODUCT UPLOAD";
			String icon = "fail";

			if (listFileStatus.isEmpty()) {
				mav.addObject("alert", alert);
				mav.addObject("icon", icon);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);
			} else {

				alert = "";
				icon = "";
				mav.addObject("report", listFileStatus);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);

				String angsuranTetap = "failed / not yet";
				String angsuranLainLain = "failed / not yet";
				String completed = "failed";

				for (int i = 0; i < listFileStatus.size(); i++) {
					if (listFileStatus.get(i).getStatus().equals("success")) {

						if (listFileStatus.get(i).getType()
								.equals("uploadCompareAngsuranTetapProduct")) {
							angsuranTetap = "success";
						}
						if (listFileStatus.get(i).getType()
								.equals("uploadCompareAngsuranLainLainProduct")) {
							angsuranLainLain = "success";
						}
					}
				}

				if ((angsuranTetap.equals("success"))
						&& (angsuranLainLain.equals("success"))) {
					completed = "success";
				}

				mav.addObject("angsuranTetap", angsuranTetap);
				mav.addObject("angsuranLainLain", angsuranLainLain);
				mav.addObject("completed", completed);
			}
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(COMPARE_PRODUCT);
		return mav;
	}

	@RequestMapping("/request-compare_product")
	public ModelAndView requestCompareProduct(ModelAndView mav,
			HttpServletRequest req, HttpServletResponse response)
			throws IOException {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String years = req.getParameter("year");

		Calendar now = Calendar.getInstance();
		int currentDate = now.get(Calendar.DATE);

		int mon = Integer.parseInt(month);
		String months = "";
		String alert = "";
		String icon = "";
		String path = "";

		currentDate = 13;

		if (currentDate >= 13) {

			if (month != null) {

				switch (mon) {
				case 1:
					months = "January";
					break;
				case 2:
					months = "February";
					break;
				case 3:
					months = "March";
					break;
				case 4:
					months = "April";
					break;
				case 5:
					months = "May";
					break;
				case 6:
					months = "June";
					break;
				case 7:
					months = "July";
					break;
				case 8:
					months = "August";
					break;
				case 9:
					months = "September";
					break;
				case 10:
					months = "October";
					break;
				case 11:
					months = "November";
					break;
				case 12:
					months = "December";
					break;
				}

				String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\CompareProduct\\"
						+ years + "\\" + months;

				File files = new File(directory);
				if (!files.exists()) {
					if (files.mkdirs()) {
					}
				}

				path = "C:/DIRECTORY_BILLING_MANAGEMENT/CompareProduct/"
						+ years + "/" + months + "/";

				alert = excelSvc.getDifferenceProduct(years, months, path,
						object.getKodeKoptelProduct(), "");

				if (alert.equals("SUCCESS COMPARE PRODUCT DATA")) {

					icon = "success";

					String file = "C:\\DIRECTORY_BILLING_MANAGEMENT\\CompareProduct\\"
							+ years + "\\" + months + "-DifferenceProduct.zip";

					File f = new File(file);
					FileInputStream fis = new FileInputStream(file);

					response.setContentType("application/xlsx");
					response.setContentLength(new Long(f.length()).intValue());

					String headerKey = "Content-Disposition";
					String headerValue = String.format(
							"attachment; filename=\"%s\"", f.getName());
					response.setHeader(headerKey, headerValue);

					OutputStream outStream = response.getOutputStream();

					byte[] buffer = new byte[4096];
					int bytesRead = -1;

					while ((bytesRead = fis.read(buffer)) != -1) {
						outStream.write(buffer, 0, bytesRead);
					}

					fis.close();
					outStream.close();

					mav.addObject("alert", alert);
					mav.addObject("icon", icon);
				} else {
					icon = "fail";
					mav.addObject("alert", alert);
					mav.addObject("icon", icon);
				}
			}
		} else {

			String type = "compareProduct";
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to compare. YOU MUST COMPARE PRODUCT ABOVE 12ND DATE.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(COMPARE_PRODUCT);
		return mav;
	}

	@RequestMapping(value = "/request-upload_final_report_product", headers = "content-type=multipart/*")
	public @ResponseBody
	ModelAndView requestUploadFinalReportProduct(ModelAndView mav,

	@RequestParam(value = "report-file") MultipartFile file,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;
		int currentDate = now.get(Calendar.DATE);

		String months = "";
		String years = "" + year;

		String alert = "";
		String icon = "";
		String path = "";

		switch (month) {
		case 1:
			months = "January";
			break;
		case 2:
			months = "February";
			break;
		case 3:
			months = "March";
			break;
		case 4:
			months = "April";
			break;
		case 5:
			months = "May";
			break;
		case 6:
			months = "June";
			break;
		case 7:
			months = "July";
			break;
		case 8:
			months = "August";
			break;
		case 9:
			months = "September";
			break;
		case 10:
			months = "October";
			break;
		case 11:
			months = "November";
			break;
		case 12:
			months = "December";
			break;
		}

		String extension = "";

		if (file.getOriginalFilename().toLowerCase().endsWith("xlsx")) {
			extension = ".xlsx";
		} else if (file.getOriginalFilename().toLowerCase().endsWith("xls")) {
			extension = ".xls";
		}

		String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\FinalReportProduct\\"
				+ years + "\\" + months;

		File files = new File(directory);
		if (!files.exists()) {
			if (files.mkdirs()) {
			}
		}

		String report = "";
		String validation = "";

		if (file.getOriginalFilename().startsWith("ReportAngsuranTetapProduct")) {
			report = "ReportAngsuranTetapProduct";

			path = "C:/DIRECTORY_BILLING_MANAGEMENT/FinalReportProduct/"
					+ years + "/" + months + "/" + report + extension;
			excelSvc.saveExcel(file, path);

			validation = excelSvc.validationReportAngsuranTetapProduct(path);
		} else {
			if (file.getOriginalFilename().startsWith(
					"ReportAngsuranLainLainProduct")) {
				report = "ReportAngsuranLainLainProduct";

				path = "C:/DIRECTORY_BILLING_MANAGEMENT/FinalReportProduct/"
						+ years + "/" + months + "/" + report + extension;
				excelSvc.saveExcel(file, path);

				validation = excelSvc
						.validationReportAngsuranLainLainProduct(path);
			} else {

				String type = "uploadFinalReportAngsuranProduct";
				String status = "failed";
				Date dates = new Date();
				path = "-";

				FileStatus fileStatus = new FileStatus(
						object.getKodeKoptelProduct(), type, months, years,
						status, path, "", dates);
				fileStatusDao.saveOrUpdate(fileStatus);

				alert = "Failed to upload. FORMAT FILENAME INCORRECT.";
				icon = "fail";
				mav.addObject("alert", alert);
				mav.addObject("icon", icon);

				// mav.addObject("user", user);
				// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
				// mav.addObject("type", typeUser);

				mav.setViewName(UPLOAD_FINAL_REPORT_PRODUCT);
				return mav;
			}
		}

		if (validation.equals("false")) {

			String type = "uploadFinal" + report;
			String status = "failed";
			Date dates = new Date();

			File filed = new File(path);
			filed.delete();
			path = "-";

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to upload. FORMAT DATA FINAL REPORT PRODUCT IS NOT MATCH WITH DEFAULT FORMAT.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);

			// mav.addObject("user", user);
			// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
			// mav.addObject("type", typeUser);

			mav.setViewName(UPLOAD_FINAL_REPORT_PRODUCT);
			return mav;
		}

		currentDate = 13;

		if (currentDate >= 13) {

			String type = "uploadFinal" + report;
			String status = "success";
			Date date = new Date();

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", date);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "UPLOAD FINAL " + report + " SUCCESS";
			icon = "success";
		} else {

			String type = "uploadFinal" + report;
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to upload. YOU MUST UPLOAD FINAL REPORT PRODUCT ABOVE 12ND DATE.";
			icon = "fail";
		}

		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(UPLOAD_FINAL_REPORT_PRODUCT);
		return mav;
	}

	@RequestMapping("/request-report_final_report_product")
	public @ResponseBody
	ModelAndView requestReportFinalReportProduct(ModelAndView mav,
			HttpServletRequest req) {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String year = req.getParameter("year");

		if (month != null) {
			int mon = Integer.parseInt(month);
			String months = "";

			switch (mon) {
			case 1:
				months = "January";
				break;
			case 2:
				months = "February";
				break;
			case 3:
				months = "March";
				break;
			case 4:
				months = "April";
				break;
			case 5:
				months = "May";
				break;
			case 6:
				months = "June";
				break;
			case 7:
				months = "July";
				break;
			case 8:
				months = "August";
				break;
			case 9:
				months = "September";
				break;
			case 10:
				months = "October";
				break;
			case 11:
				months = "November";
				break;
			case 12:
				months = "December";
				break;
			}

			List<FileStatus> listFileStatus = excelSvc.findFinalReportProduct(
					months, year);
			String alert = "NO FILE FINAL REPORT PRODUCT UPLOAD";
			String icon = "fail";

			if (listFileStatus.isEmpty()) {
				mav.addObject("alert", alert);
				mav.addObject("icon", icon);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);
			} else {

				alert = "";
				icon = "";
				mav.addObject("report", listFileStatus);
				mav.addObject("month", month);
				mav.addObject("m", months);
				mav.addObject("y", year);

				String angsuranTetap = "failed / not yet";
				String angsuranLainLain = "failed / not yet";
				String completed = "failed";

				for (int i = 0; i < listFileStatus.size(); i++) {
					if (listFileStatus.get(i).getStatus().equals("success")) {

						if (listFileStatus
								.get(i)
								.getType()
								.equals("uploadFinalReportAngsuranTetapProduct")) {
							angsuranTetap = "success";
						}
						if (listFileStatus
								.get(i)
								.getType()
								.equals("uploadFinalReportAngsuranLainLainProduct")) {
							angsuranLainLain = "success";
						}
					}
				}

				if ((angsuranTetap.equals("success"))
						&& (angsuranLainLain.equals("success"))) {
					completed = "success";
				}

				mav.addObject("angsuranTetap", angsuranTetap);
				mav.addObject("angsuranLainLain", angsuranLainLain);
				mav.addObject("completed", completed);
			}
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(SPLIT_PRODUCT);
		return mav;
	}

	@RequestMapping("/request-split_product")
	public @ResponseBody
	ModelAndView requestSplitProduct(ModelAndView mav, HttpServletRequest req,
			HttpServletResponse response) throws IOException {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String years = req.getParameter("year");

		Calendar now = Calendar.getInstance();
		int currentDate = now.get(Calendar.DATE);

		int mon = Integer.parseInt(month);
		String months = "";
		String alert = "";
		String icon = "";
		String path = "";

		currentDate = 13;

		if (currentDate >= 13) {

			if (month != null) {

				switch (mon) {
				case 1:
					months = "January";
					break;
				case 2:
					months = "February";
					break;
				case 3:
					months = "March";
					break;
				case 4:
					months = "April";
					break;
				case 5:
					months = "May";
					break;
				case 6:
					months = "June";
					break;
				case 7:
					months = "July";
					break;
				case 8:
					months = "August";
					break;
				case 9:
					months = "September";
					break;
				case 10:
					months = "October";
					break;
				case 11:
					months = "November";
					break;
				case 12:
					months = "December";
					break;
				}

				String directory = "C:\\DIRECTORY_BILLING_MANAGEMENT\\FinalReportProduct\\"
						+ years + "\\" + months;

				File files = new File(directory);
				if (!files.exists()) {
					if (files.mkdirs()) {
					}
				}
				path = "C:/DIRECTORY_BILLING_MANAGEMENT/FinalReportProduct/"
						+ years + "/" + months + "/";

				alert = excelSvc.getSplitFinalReportProduct(years, months,
						path, object.getKodeKoptelProduct(), "");

				icon = "";

				if (alert.equals("SUCCESS SPLIT FINAL REPORT PRODUCT")) {
					icon = "success";
				} else {
					if (alert
							.equals("Failed to split. FILE FINAL REPORT PRODUCT IS NOT FOUND IN DIRECTORY.")) {
						icon = "fail";
					}
				}

				mav.addObject("alert", alert);
				mav.addObject("icon", icon);
			}
		} else {

			String type = "splitProduct";
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to split. YOU MUST SPLIT PRODUCT ABOVE 12ND DATE.";
			icon = "fail";
			mav.addObject("alert", alert);
			mav.addObject("icon", icon);
		}

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(SPLIT_PRODUCT);
		return mav;
	}

	@RequestMapping("/request-download_final_report_product")
	public ModelAndView requestDownloadFinalReportProduct(ModelAndView mav,
			HttpServletRequest req, HttpServletResponse response)
			throws IOException {

		session = req.getSession();
		if (session == null) {
			mav.setViewName(BILLING);
			return mav;
		}
		if (session.getAttribute("logindata") == null) {
			mav.setViewName(BILLING);
			return mav;
		}

		SessionDataObject object = (SessionDataObject) session
				.getAttribute("logindata");
		session.setAttribute("logindata", object);

		String month = req.getParameter("month");
		String years = req.getParameter("year");

		Calendar now = Calendar.getInstance();
		int currentDate = now.get(Calendar.DATE);

		int mon = Integer.parseInt(month);
		String months = "";
		String alert = "";
		String icon = "";
		String path = "";

		currentDate = 13;

		if (currentDate >= 13) {

			if (month != null) {

				switch (mon) {
				case 1:
					months = "January";
					break;
				case 2:
					months = "February";
					break;
				case 3:
					months = "March";
					break;
				case 4:
					months = "April";
					break;
				case 5:
					months = "May";
					break;
				case 6:
					months = "June";
					break;
				case 7:
					months = "July";
					break;
				case 8:
					months = "August";
					break;
				case 9:
					months = "September";
					break;
				case 10:
					months = "October";
					break;
				case 11:
					months = "November";
					break;
				case 12:
					months = "December";
					break;
				}

				String file = "C:/DIRECTORY_BILLING_MANAGEMENT/SplitFinalReportProduct/"
						+ years
						+ "/"
						+ months
						+ "/"
						+ object.getKodeKoptelProduct()
						+ "-FinalReportProduct.zip";

				File f;
				FileInputStream fis;

				try {

					f = new File(file);
					fis = new FileInputStream(file);

					response.setContentType("application/xlsx");
					response.setContentLength(new Long(f.length()).intValue());

					String headerKey = "Content-Disposition";
					String headerValue = String.format(
							"attachment; filename=\"%s\"", f.getName());
					response.setHeader(headerKey, headerValue);

					OutputStream outStream = response.getOutputStream();

					byte[] buffer = new byte[4096];
					int bytesRead = -1;

					while ((bytesRead = fis.read(buffer)) != -1) {
						outStream.write(buffer, 0, bytesRead);
					}

					fis.close();
					outStream.close();

					String type = "downloadFinalReportProduct";
					String status = "success";
					Date dates = new Date();

					FileStatus fileStatus = new FileStatus(
							object.getKodeKoptelProduct(), type, months, years,
							status, file, "", dates);
					fileStatusDao.saveOrUpdate(fileStatus);

					alert = "SUCCESS DOWNLOAD FINAL REPORT PRODUCT";
					icon = "success";

				} catch (Exception e) {

					String type = "downloadFinalReportProduct";
					String status = "failed";
					Date dates = new Date();
					path = "-";

					FileStatus fileStatus = new FileStatus(
							object.getKodeKoptelProduct(), type, months, years,
							status, path, "", dates);
					fileStatusDao.saveOrUpdate(fileStatus);

					System.out
							.println("Failed to download. FINAL REPORT PRODUCT IS NOT FOUND IN DIRECTORY. PLEASE SPLIT PRODUCT BY ADMIN.");

					alert = "Failed to download. FINAL REPORT PRODUCT IS NOT FOUND IN DIRECTORY. PLEASE SPLIT PRODUCT BY ADMIN.";
					icon = "fail";
				}
			}
		} else {

			String type = "downloadFinalReportProduct";
			String status = "failed";
			Date dates = new Date();
			path = "-";

			FileStatus fileStatus = new FileStatus(
					object.getKodeKoptelProduct(), type, months, years, status,
					path, "", dates);
			fileStatusDao.saveOrUpdate(fileStatus);

			alert = "Failed to download. YOU MUST DOWNLOAD PRODUCT ABOVE 12ND DATE.";
			icon = "fail";
		}

		mav.addObject("alert", alert);
		mav.addObject("icon", icon);

		// mav.addObject("user", user);
		// mav.addObject("kodeKoptelProduct", kodeKoptelProduct);
		// mav.addObject("type", typeUser);

		mav.setViewName(DOWNLOAD_FINAL_REPORT_PRODUCT);
		return mav;
	}

}
